﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HydroDesktop.WebServices;
using HydroDesktop.Database;
using HydroDesktop.Interfaces;
using HydroDesktop.Configuration;
using System.IO;
using System.Reflection;

namespace HydroDesktop.Search
{
    public partial class AdvancedSettingsDialog : Form
    {
		#region Private Member Variables

		//private readonly string SERVICES_XML_NAME = Properties.Settings.Default.SERVICES_XML_NAME;
		private static readonly string _ontologyFilename = Properties.Settings.Default.ontologyFilename;

		#endregion

        public AdvancedSettingsDialog()
        {
            InitializeComponent();

            //to get the URL
            string url = Settings.Instance.SelectedHISCentralURL;

            //to restore the settings:
            if (url == "http://water.sdsc.edu/hiscentral/webservices/hiscentral.asmx")
            {
                radioButton3.Checked = true;
            }
            else if (url == "http://hiscentral.cuahsi.org/webservices/hiscentral.asmx")
            {
                radioButton4.Checked = true;
            }
            else
            {
                radioButton7.Checked = true;
                textBox3.Text = url;
            }

            //to restore download options settings
            
            switch (Settings.Instance.DownloadOption)
            {
                case "Append":
                case "Fill":
                    rbAppend.Checked = true;
                    break;
                case "Copy":
                    rbCopy.Checked = true;
                    break;
                case "Overwrite":
                    rbOverwrite.Checked = true;
                    break;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {      
            string hisCentralURL = "http://water.sdsc.edu/hiscentral/webservices/hiscentral.asmx";
            
            if (radioButton3.Checked)
            {
                hisCentralURL = "http://water.sdsc.edu/hiscentral/webservices/hiscentral.asmx";
            }
            else if (radioButton4.Checked)
            {
                hisCentralURL = "http://hiscentral.cuahsi.org/webservices/hiscentral.asmx";
            }
            else
            {
                //check if it is a valid URL
                hisCentralURL = textBox3.Text;
                try
                {
                    HISCentralClient client = new HISCentralClient(hisCentralURL);
                    hisCentralURL = textBox3.Text;
                }
                catch
                {
                    MessageBox.Show("The URL is not valid. Please enter a valid HIS Central URL.");
                    this.DialogResult = DialogResult.None;
                }
            }
            lblHisCentralURL.Text = hisCentralURL;
            Settings.Instance.SelectedHISCentralURL = hisCentralURL;

            //to save the settings
            if (rbAppend.Checked)
            {
                Settings.Instance.DownloadOption = OverwriteOptions.Append.ToString();
            }
            else if (rbCopy.Checked)
            {
                Settings.Instance.DownloadOption = OverwriteOptions.Copy.ToString();
            }
            else if (rbOverwrite.Checked)
            {
                Settings.Instance.DownloadOption = OverwriteOptions.Overwrite.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HISCentralSearcher searcher = new HISCentralSearcher(Settings.Instance.SelectedHISCentralURL);
            //string xmlFileName = Path.Combine ( Application.StartupPath, SERVICES_XML_NAME );
            //searcher.GetWebServicesXml(xmlFileName);
            WebServicesList servicesList = new WebServicesList();
            servicesList.RefreshListFromHisCentral(searcher);
        }

        private void btnRefreshKeywords_Click(object sender, EventArgs e)
        {
            HISCentralSearcher searcher = new HISCentralSearcher(Settings.Instance.SelectedHISCentralURL);
            string pluginPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			string xmlFileName = Path.Combine ( pluginPath, _ontologyFilename );
            searcher.GetOntologyTreeXml(xmlFileName);
        }
    }
}
